<?php
session_start();
function uploadNewNameImage($file = '')
{
    $NewName = '';
    if (strlen($file)) {
        $name = $_FILES[$file]['name'];
        list($txt, $ext) = explode(".", $name);
        $valid_formats = array(
            "jpeg",
            "jpg",
            "png",
            "gif",
            "bmp");
        if (in_array($ext, $valid_formats)) {
            $NewName = substr(md5(time() . substr(str_replace(" ", "_", $txt), 5)), 7, 20) .
                "." . $ext;
        }
    }
    return $NewName;
}

function uploadImages($MyFiles = 'HinhNho', $UploadDirectory = 'upload/')
{
    if(!isset($_FILES[$MyFiles]['name']))
    {
        $_SESSION['Loi'] = '';
        return;
    }
    $_SESSION[$MyFiles] = array();
    $valid_formats = array(
        ".jpeg",
        ".jpg",
        ".png",
        ".gif",
        ".bmp");
    for ($i = 0; $i < count($_FILES[$MyFiles]['name']); $i++) {
        if ($_FILES[$MyFiles]['error'][$i]) {
            $_SESSION['Loi'] = uploadErrors($_FILES[$MyFiles]['error'][$i]);
        }
        $FileName = strtolower($_FILES[$MyFiles]['name'][$i]); //uploaded file name
        $ImageExt = substr($FileName, strrpos($FileName, '.')); //file extension
        $FileType = $_FILES[$MyFiles]['type'][$i]; //file type
        $FileSize = $_FILES[$MyFiles]['size'][$i]; //file size
        $RandNumber = rand(0, 9999999999);
        $UploadDate = date("Y-m-d H:i:s");
        $MidName = substr(md5($RandNumber), 0, 6);
        $UploadDate = substr(md5($UploadDate), 0, 6);
        $NumberName = substr(md5($i . $UploadDate), 0, 6);
        $EndName = substr(md5($UploadDate . $NumberName), 0, 6);
        $NewFileName = $MyFiles . "_" . $MidName . $EndName . $NumberName . $UploadDate .
            $ImageExt; //$_FILES[$MyFiles]['name'][$i];
        if (trim($_FILES[$MyFiles]['name'][$i]) != '') {
            if (in_array($ImageExt, $valid_formats)) {
                if (move_uploaded_file($_FILES[$MyFiles]['tmp_name'][$i], $UploadDirectory . $NewFileName)) {
                    $_SESSION[$MyFiles][] = $NewFileName;
                }
            }
        }
    }
}

function uploadImage($file = '', $dir = 'images/upload/', $newName = '')
{
    $tr_err = '';
    if (strlen($file)) {
        $name = $_FILES[$file]['name'];
        list($txt, $ext) = explode(".", $name);
        $valid_formats = array(
            "jpeg",
            "jpg",
            "png",
            "gif",
            "bmp");
        if (in_array($ext, $valid_formats)) {
            if ($_FILES[$file]["error"] > 0) {
                $tr_err = "Error: " . $_FILES[$file]["error"] . "<br>";
                return $tr_err;
            } else {
                $tmp = $_FILES[$file]["tmp_name"];
                move_uploaded_file($tmp, $dir . $newName);
                return 'yes';
            }
        } else {
            $tr_err = "Sai định dạng ảnh! Chỉ chấp nhận định dạng: ";
            $kt = 0;
            $num = count($valid_formats) - 1;
            foreach ($valid_formats as $k => $v) {
                if ($kt == $num)
                    $tr_err .= $v . ".";
                else
                    $tr_err .= $v . ", ";
                $kt++;
            }
            return $tr_err;
        }
    } else
        $tr_err = "Chưa chọn file upload...";

    return $tr_err;
}

function uploadErrors($err_code)
{
	switch ($err_code)
    { 
        case UPLOAD_ERR_INI_SIZE: 
            return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; 
        case UPLOAD_ERR_FORM_SIZE: 
            return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; 
        case UPLOAD_ERR_PARTIAL: 
            return 'The uploaded file was only partially uploaded'; 
        case UPLOAD_ERR_NO_FILE: 
            return 'No file was uploaded'; 
        case UPLOAD_ERR_NO_TMP_DIR: 
            return 'Missing a temporary folder'; 
        case UPLOAD_ERR_CANT_WRITE: 
            return 'Failed to write file to disk'; 
        case UPLOAD_ERR_EXTENSION: 
            return 'File upload stopped by extension'; 
        default: 
            return 'Unknown upload error'; 
    } 
}

/**
 * renameFile()
 * 
 * @param string $file
 * @param string $fileNew
 * @return void
 */
function renameFile($file = '', $fileNew = '')
{
    if ($file != '' && $fileNew != '')
        rename($file, $fileNew);
}

/**
 * deleteFile()
 * 
 * @param string $file
 * @param string $dir
 * @return void
 */
function deleteFile($file = '', $dir = '')
{
    $pathFile = $dir . $file;
    if (@file_exists($pathFile)) {
        return @unlink($pathFile);
    }
    return false;
}

function deleteDirectory($dirname = '')
{
    if (is_dir($dirname))
        $dir_handle = opendir($dirname);
    if (!$dir_handle)
        return false;
    while ($file = readdir($dir_handle)) {
        if ($file != "." && $file != "..") {
            if (!is_dir($dirname . "/" . $file))
                unlink($dirname . "/" . $file);
            else
                delete_directory($dirname . '/' . $file);
        }
    }
    closedir($dir_handle);
    rmdir($dirname);
    return true;
}

function createDirectory($dir = '')
{
    if ($dir != '') {
        if (!file_exists($dir)) {
            @mkdir($dir);
        }
    }
}
?>